<template>
	<!-- 定期排查填写流程页面 -->
	<view class="workApply-container" :style="{ height: `${windowHeight}px` }">
		<!-- 基础表单校验 -->
		<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" border>
			<uni-forms-item label="公司名称：" required name="deptId">

				<custom-tree-select :listData="treeData" disabled dataLabel="label" dataValue="id" :search="true"
					:mutiple="false" v-model="valiFormData.deptId" />

				</custom-tree-select>

			</uni-forms-item>

			<uni-forms-item label="隐患名称：" required name="hiddenDangerName">
				<uni-easyinput v-model="valiFormData.hiddenDangerName" placeholder="请输入" :inputBorder="false" />
			</uni-forms-item>
			<!-- 上面选择检查计划回写上来的 -->
			<uni-forms-item label="隐患等级：" required name="hiddenDangerLevel">
				<uni-data-checkbox v-model="valiFormData.hiddenDangerLevel"
					:localdata="hiddenLevelOption"></uni-data-checkbox>
			</uni-forms-item>

			<!-- 上面选择检查计划回写上来的 -->
			<uni-forms-item label="隐患描述：" required name="hiddenDangerInfo">
				<uni-easyinput type="textarea" v-model="valiFormData.hiddenDangerInfo" placeholder="请输入"
					:inputBorder="false" />

			</uni-forms-item>

			<uni-forms-item label="上传文件：" required name="">
				<uni-file-picker limit="5" file-mediatype="all" disable-preview @select="select" @delete="deleteFile"
					@success="success"></uni-file-picker>

			</uni-forms-item>
		</uni-forms>

		<button type="primary" @click="submit('valiForm')" class="submit-btn">
			确认
		</button>


	</view>
</template>

<script>
	import widowHeightMixins from "@/mixins/widowHeightMixins.js";
	import ManualSelect from "../otherViews/modal/manualSelect.vue"
	import {
		getNowTime
	} from "@/utils/nowTime.js"
	import {
		addAllShoot,
	} from "@/api/hiddenTrouble/allShoot.js";
	import {
		listCheckPlan,
		getCheckPlan

	} from "@/api/hiddenTrouble/checkPlan.js"
	import {
		getInfo,
		listUser
	} from "@/api/system/user.js"
	import {
		deptTreeSelect
	} from "@/api/system/user";
	import {
		mapGetters
	} from "vuex"
	export default {
		mixins: [widowHeightMixins],
		components: {

		},
		onLoad(e) {
			// console.log(e, "e");
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('acceptDataFromOpenedPage', (data) => {
				this.queryForm.pageStatus = data.pageStatus
				// console.log("接受", data)
				this.detailObj = data.totalObj
				this.operateStatus = data.operateStatus
				this.checkId = data.checkId
				if (data.operateStatus == 1) {
					this.handleopen()
				}

			});
			// 动态设置标题
			uni.setNavigationBarTitle({
				title: `全员排查--新增`,
			});
		},
		data() {
			return {
				//隐患登记
				hiddenForm: {},
				//详情
				detailObj: {},
				hiddenLevelOption: [{
						text: '一般隐患',
						value: 1
					},
					{
						text: '重大隐患',
						value: 2,
						disable:true
					},

				],
				solveOption: [{
						text: '是',
						value: 1
					},
					{
						text: '否',
						value: 2
					},
				],
				//文件的数组
				fileInfoList: [],

				//选中ID
				checkId: null,
				//操作状态
				operateStatus: null,
				//上传参数对象
				queryForm: {},

				//被检查单位的数据
				treeData: [],
				// 校验表单数据
				valiFormData: {},

				// 数据总量
				total: 0,
				loading: false,



				// 校验规则
				rules: {
					dept: {
						rules: [{
							required: true,
							errorMessage: "姓名不能为空",
						}, ],
					},
					name: {
						rules: [{
							required: true,
							errorMessage: "姓名不能为空",
						}, ],
					},
					age: {
						rules: [{
								required: true,
								errorMessage: "年龄不能为空",
							},
							{
								format: "number",
								errorMessage: "年龄只能输入数字",
							},
						],
					},
				},
			};
		},
		methods: {
			removeObjectById(arr, id) {
				let index = arr.findIndex(obj => obj.fileId === id);
				if (index !== -1) {
					arr.splice(index, 1);
				}
				return arr;
			},


			//删除上传状态
			deleteFile(e) {
				let file = e.tempFile
				let arr = this.removeObjectById(this.fileInfoList, file.uuid)


			},
			// 获取上传状态
			select(e) {
				console.log('选择文件：', e)
				let file = e.tempFiles[0]

				this.fileInfoList.push({
					fileId: file.uuid,
					fileName: file.name,
					type: 13,
					url: file.url
				})
				console.log("新增数组的", this.fileInfoList)
			},


			// 上传成功
			success(e) {
				console.log('上传成功', e)
			},


			//登记按钮（打开抽屉）
			handleResigration(row) {
				// this.$refs['showLeft'].open()
				uni.navigateTo({
					url: `/pages/trouble/process/registration`,
					success: (res) => {
						res.eventChannel.emit('registrationAcct', {
							detail: this.detailObj.summaryTwo,
							checkId: this.checkId,
							tableId: row.id
						})
					}
				})
			},
			//关闭抽屉
			closeDrawer() {
				this.$refs['showLeft'].close()
			},


			//处理详情接口数据回写上面
			handleopen(data) {

			},
			/** 被检查部门部门下拉树结构 */
			async getDeptTree() {
				const response = await deptTreeSelect();
				this.treeData = response.data;
			},

			init() {
				let res = uni.getStorageSync('userInfo')

				this.valiFormData.deptId = [res.deptId]
			},
			submit(ref) {
				this.$refs[ref]
					.validate()
					.then(async (res) => {
						this.valiFormData.deptId = this.valiFormData.deptId.toString()

						this.valiFormData.fileInfoList = this.fileInfoList
						let response = await addAllShoot(this.valiFormData);
						uni.showToast({
							title: `提交成功`,
							duration: 2000
						});
						uni.navigateBack()


					})
					.catch((err) => {
						console.log("err", err);
					});
			},
		},
		mounted() {
			this.getDeptTree()
			this.init()
		},
		computed: {
			...mapGetters(["allUsers", ]),
		}
	};
</script>

<style lang="scss" scoped>
	.workApply-container {
		padding: 20rpx;
		background-color: #fff;
		overflow-y: auto;

		::v-deep {
			.uni-forms-item--border {
				border-top: 0 solid #000;
				border-bottom: 2rpx solid #e8eaec;
			}

			.uni-forms-item__label {
				width: 40% !important;
			}

			.submit-btn {
				margin-top: 30rpx;
				background: #137ee9;
				border-radius: 10rpx;
			}
		}
	}
</style>